package com.qf.wemedia.controller;

import com.qf.common.response.ResponseEnum;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class LoginController {
    /**
     * 前往登录页面
     * @return
     */
    @RequestMapping({"/","login"})
    public String toLogin(){
        return "login";
    }

    /**
     * 以shiro方式验证登录信息
     * @param model
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("login.do")
    public String login(Model model, @RequestParam("username") String username,@RequestParam("password") String password){
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        Subject subject = SecurityUtils.getSubject();
        try{
            subject.login(token);
            if (subject.isAuthenticated()){
                return "redirect:index";
            }
        }catch (Exception e){
        }
        model.addAttribute("error", ResponseEnum.LOGIN_FAIL.getMessage());
        return "login";
    }
}
